import socket
import pickle

# 服务器地址和端口
SERVER_IP = '127.0.0.1'
SERVER_PORT = 5555

# 保存最高分
high_score = 0

def handle_client(client_socket):
    global high_score
    while True:
        try:
            data = client_socket.recv(4096)
            if not data:
                break
            scores = pickle.loads(data)
            if scores > high_score:
                high_score = scores
            client_socket.sendall(pickle.dumps(high_score))
        except:
            break
    client_socket.close()

def main():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((SERVER_IP, SERVER_PORT))
    server.listen(5)
    print(f'Server listening on {SERVER_IP}:{SERVER_PORT}')

    while True:
        client_socket, addr = server.accept()
        print(f'Accepted connection from {addr}')
        handle_client(client_socket)

if __name__ == '__main__':
    main()
